import time
from . import ConnPG
import psycopg2


class AddDuty:

    async def add_duty(self, duty_name):#添加职务

        #参数检查

        if not duty_name:

            return {
                'status': -1,
                'errorCode': 104100,
                'errorMsg': '职务不能为空',
                'resultBody': None
            }

        sql = 'INSERT INTO duty(duty_name) VALUES (%s)'

        try:
            await ConnPG().execute(sql, (duty_name,))
        except psycopg2.errors.UniqueViolation:

            return {

                'status': -1,
                'errorCode': 104101,
                'errorMsg': '职务已经存在',
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "职务添加成功",
            }

    async def user_add_duty(self, user_id, duty_id):#添加职务

        #参数检查


        if not (user_id.isdecimal() and duty_id.isdecimal()):

            return {
                'status': -1,
                'errorCode': 104100,
                'errorMsg': '两个参数类型应为整数',
                'resultBody': None
            }

        user_id, duty_id = int(user_id), int(duty_id)

        #检查是否库中已经存在该关系
        # check_sql = 'select * from user_duty where user_id=%s and duty_id=%s and drop_time=null'
        # check_result = await ConnPG().fetchone(check_sql, (user_id, duty_id))
        
        check_sql = 'select * from user_duty where user_id=%s and duty_id=%s and drop_time is null'
        check_result = await ConnPG().fetchone(check_sql, (user_id, duty_id))

        if check_result:

            return {
                'status': -1,
                'errorCode': 104102,
                'errorMsg': '该成员已经是此职务',
                'resultBody': None
            }

        sql = 'INSERT INTO user_duty(user_id, duty_id, create_time) VALUES (%s, %s, %s)'

        await ConnPG().execute(sql, (user_id, duty_id, int(time.time())))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "职务添加成功",
        }

    async def update_duty(self, duty_id, new_name):#职务更名

        #参数检查
        if duty_id == None or not duty_id.isdecimal() or new_name == None:

            return {
                'status': -1,
                'errorCode': 104100,
                'errorMsg': '接口有两个个必传参数，其中duty_id为整数',
                'resultBody': None
            }

        sql = "UPDATE duty SET duty_name=%s WHERE id=%s"

        await ConnPG().execute(sql, (new_name, duty_id))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': f"职务名称更新为：{new_name}",
        }


    async def del_duty(self, duty_id):#删除用户

        #参数检查
        if duty_id == None or not duty_id.isdecimal():



            return {
                'status': -1,
                'errorCode': 104100,
                'errorMsg': '接口有一个必传参数且duty_id为整数',
                'resultBody': None
            }

        sql = "DELETE FROM duty WHERE id=%s"

        try:
            await ConnPG().execute(sql, (duty_id,))
        except psycopg2.errors.ForeignKeyViolation as e:

            print(e)

            return {

                'status': -1,
                'errorCode': 104112,
                'errorMsg': str(e),
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "职务删除成功",
            }